package com.jnu.student;

import android.os.Handler;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class ShopLoader {
    private String message;
    private ArrayList<Shop> shops;

    /*******************************************
        downLoad() - get json String from spec
        spec – the String to parse as a URL.
     *******************************************/
    public String downLoad(String spec){
        try {
            //建立链接
            URL url = new URL(spec);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            //设置连接属性
            httpURLConnection.setConnectTimeout(5000);//设置超时
            httpURLConnection.setUseCaches(false);
            httpURLConnection.connect();

            //成功建立链接后，通过文件流读取信息
            if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                //建立输入流及其读取器，并通过BufferedReader缓冲读取
                InputStream inputStream = httpURLConnection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                StringBuffer stringBuffer = new StringBuffer();
                String line = "";

                while (null != (line = bufferedReader.readLine())) {
                    stringBuffer.append(line);
                }
                message = stringBuffer.toString();
                Log.v("out---------------->", message);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }

    /***********************************************************
     parseJson() - parse json String and return shops Arraylist
     content – the Json String of online information
     ***********************************************************/
    public ArrayList<Shop> parseJson(String content) {
        ArrayList<Shop> parseResult = new ArrayList<>();
        if (null != content) {
            try {
                JSONObject jsonObject = new JSONObject(content);
                JSONArray jsonData = jsonObject.getJSONArray("shops");
                for (int index = 0; index < jsonData.length(); index++) {
                    JSONObject shopJson = jsonData.getJSONObject(index);
                    Shop shop = new Shop();
                    shop.setName(shopJson.getString("name"));
                    shop.setLatitude(shopJson.getDouble("latitude"));
                    shop.setLongitude(shopJson.getDouble("longitude"));
                    shop.setMemo(shopJson.getString("memo"));
                    parseResult.add(shop);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return parseResult;
    }

    /************************************************************
     load() - create a thread to load json message from url
     handler – Receives a message when the child thread completes
     url - the place save the information
     ************************************************************/
    public void load(Handler handler, String url) {
        new Thread(() -> {
            String content = downLoad(url);
            shops = parseJson(content);
            handler.sendEmptyMessage(1);
        }).start();
    }

    public ArrayList<Shop> getShops(){
        return shops;
    }
}
